package com.datviet.service.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.datviet.common.Constants;
import com.datviet.dao.IRoleDAO;
import com.datviet.dao.IUserDAO;
import com.datviet.domain.Message;
import com.datviet.domain.UserRegisterDto;
import com.datviet.entity.Role;
import com.datviet.entity.User;
import com.datviet.service.common.security.PasswordEncoderFacade;
import com.datviet.service.user.IUserRegisterService;

@Service
public class UserRegisterServiceImpl implements IUserRegisterService {

	@Autowired
	private IUserDAO userDao;	
	
	@Autowired
	private IRoleDAO roleDao;
	
	@Transactional
	@Override
	public Message registerUser(UserRegisterDto userRegister) {
		Message message = new Message();
		User newUser = new User();
		Role role = roleDao.findRoleByMatchedName(Constants.ROLE.USER);
		if(role != null){
			newUser.addRole(role);
		}
		newUser.setUsername(userRegister.getUsername());
		newUser.setPassword(PasswordEncoderFacade.encode(userRegister.getPassword()));
		userDao.create(newUser);
		message.setMessage("Register successfully");
		return message;
	}

}
